<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../CSS/default.css" />
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">

	</head>

	<body style="background-color:white;padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<form id="ff" method="post" style="margin-left: 15%;">
			<div style="display: none;">
				<input id="input_ID" class="easyui-textbox" type="text" name="ID" style="width:50%;"></input>
			</div>
			<table cellpadding="3" class="displaytable" style="padding: 20px;">
				<tr>
					<td width="10%">
						<h id="star">* </h>短信类型
					</td>
					<td width="40%">
						<input id="input_TYPE" class="easyui-combobox" name="TYPE" style="width:60%" data-options="required:true"></input>
					</td>
					<td width="10%">
						<h id="star">* </h>发送时间
					</td>
					<td width="40%">
						<div id="input_SENDDATE" class="easyui-datebox" name="SENDDATE" style="width:60%" data-options="required:true"></div>
					</td>
				</tr>
				<tr>
					<td>
						<h id="star">* </h>对象分组
					</td>
					<td>
						<div id="input_GROUP"></div>
					</td>
					<td>
						选择短信模板
					</td>
					<td>
						<input id="input_SMSMdoule" class="easyui-combobox" name="SMSMdoule" style="width:30%"></input>
					</td>
				</tr>
				<tr>
					<td colspan="4">
						<div id="smsContent"></div>
					</td>
				</tr>
			</table>
		</form>
		<div id="tt"></div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/MarketShareFunction/SMSShareFunction.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
	<script type="text/javascript">
		var currGroupType = ""; //当前发送短信模式
		var currGroupValue ;
		var sendType ;
		var receipts_type = RECEIPT_SAVE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/
		var s = IniSMSContent("CONTEXT");
		$("#smsContent").append(s);

		function save() {
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "有必填项没有填写");
				return;
			}
			SaveData();
		}

		function cls() {
			$('#ff').form('reset');
		}

		function SaveData() {
			var _url = '';
			var data = new Object();
			data.SENDDATE = $("#input_SENDDATE").datebox("getValue").trim();
			data.CONTEXT = $("#input_CONTEXT").textbox("getValue");
			data.TELS = "";
			data.NAMES = "";
			data.TYPE = $('#input_TYPE').combobox("getValue");
			if(currGroupValue == "选择部分") {
				var dataGridRows = $("#tt").datagrid("getRows");
				for(var i = 0; i < dataGridRows.length; i++) {
					if(dataGridRows[i].TELEPHONE != undefined) {
						data.NAMES=data.NAMES+dataGridRows[i].NAME+";";
						data.TELS = data.TELS + dataGridRows[i].TELEPHONE + ";";
					}
				}
				_url = RemoteLinkAddress + "/phonesms/send.do";
			} else {
				data.NAME = sendType;
				_url = RemoteLinkAddress + "/phonesms/sendall.do";
			}
			$.post(_url, data,
				function(result) {
					if(result.msg.search("success") > -1) {
						$.messager.alert({ // show error message
							title: '成功',
							msg: '添加成功'
						});
					} else {
						$.messager.alert({ // show error message
							title: '错误',
							msg: result.msg
						});
					}
				},
				'json');
		}
		
		var currComboData = $.grep(SMSType, function(value) {
			return value.group == "即时短信"; //筛选出即时短信
		});
		
		$('#input_TYPE').combobox({
			data: currComboData,
			panelHeight: 80,
			valueField: 'id',
			textField: 'text',
			panelHeight: 'auto',
		});

		$('#input_GROUP').combobox({
			data: [{
				text: "选择部分",
				value: "All",
			}, {
				text: "全体租户",
				value: "0",
			}, {
				text: "全体小商户",
				value: "1",
			}, {
				text: "所有人员",
				value: "2",
			}, ],
			method: 'get',
			valueField: "value",
			textField: "text",
			lines: true,
			width: '60%',
			panelHeight: 'auto',
			singleSelect: true,
			onSelect: function(result) {
				currGroupValue = result.text;
				sendType = result.value;
			}
		});

		$("#input_SMSMdoule").combobox({
			valueField: 'NAME',
			textField: 'NAME',
			mode: 'remote',
			method: 'get',
			width: '60%',
			panelHeight: 'auto',
			url: RemoteLinkAddress + '/smstemplate/listall.do',
			onSelect: function(result) {
				$("#input_CONTEXT").textbox('setValue', result.CONTEXT);
			}
		});

		var _detailToolbarData = AddDetailToolBar(7);
		GetTable();
		var currRow;

		function GetTable() {
			$('#tt').edatagrid({
				height: 290,
				width: '100%',
				toolbar: _detailToolbarData,
				collapsible: true,
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
					[{
						field: 'TYPE',
						title: '客户类型',
						width: '20%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								panelHeight: 'auto',
								valueField: 'text',
								textField: 'text',
								data: [{
									'text': '租户',
								}, {
									'text': '小商户',
								}],
								onSelect: function(record) {
									switch(record.text) {
										case "租户":
											currGroupType = 0;
											break;
										case "小商户":
											currGroupType = 1;
											break;
									}
								}
							}
						}
					}, {
						field: 'NAME',
						title: '客户名',
						width: '20%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: 'NAME',
								textField: 'NAME',
								panelWidth: 200,
								panelHeight: 'auto',
								mode: 'remote',
								method: 'get',
								onChange: function(newValue, oldValue) {
									if(newValue != "" && newValue != undefined) {
										console.log("客户类型" + currGroupType);
										if(currGroupType.toString() == "") {
											$.messager.alert("警告", "请选择客户类型");
											return;
										}
										var url = RemoteLinkAddress + "/market/tenant/findbypinyin?TYPE=" + currGroupType + "&PINYINFIELDNAME=" + newValue.trim();
										console.log(url);
										$(this).combobox("reload", url);
									}
								},
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									console.log(rowIndex);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'TELEPHONE'
									}).target;
									target.textbox('clear');
									if(record.TELEPHONE != undefined) {
										target.textbox('setValue', record.TELEPHONE);
									}
								}
							}
						},
					}, {
						field: 'TELEPHONE',
						title: '手机号码',
						width: '40%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "TELEPHONE",
								textField: "TELEPHONE",
								editable: true,
								required: true
							}
						}
					}]
				],
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				}
			});
		}

		function getNowFormatDate() {
			var date = new Date();
			var seperator1 = "/";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
			return currentdate;
		}
	</script>

</html>